package com.linked.universal.common;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.time.LocalDateTime;

/**
 * @author :dbq
 * @date : 2022/10/25 14:06
 */
@Data
public class LinkedToken {
    /**
     * token
     * 对当前对象进行加密，里面包含userId、expirTime，
     * 解密以后，用户与用户id,过期时间与过期时间做对比，
     * 判断当前数据是否是有效数据
     */
    @JsonProperty("token")
    private String token;
    /**
     * 失效时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonProperty("expirTime")
    private LocalDateTime expirTime;

    /**
     * 用户id
     */
    @JsonProperty("userId")
    private String userId;


    public LinkedToken(String token, Integer validHour) {
        this.token = token;
        if (validHour == null) {
            validHour = 3;
        }
        this.expirTime = LocalDateTime.now().plusHours(validHour);
    }

    public LinkedToken(String userId) {
        this.expirTime = LocalDateTime.now().plusHours(3);
        this.userId = userId;
    }

    public LinkedToken(LocalDateTime expirTime, String userId) {
        this.expirTime = expirTime.plusHours(3);
        this.userId = userId;
    }

    public LinkedToken() {
    }
}
